package com.suning.sawp.service.impl.mainpushnew;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.intf.dao.mainpushnew.StaffTaskDao;
import com.suning.sawp.intf.mainpushnew.StaffMainPushService;
import com.suning.sawp.po.mainpushnew.StaffModelRel;
import com.suning.sawp.po.mainpushnew.StoreModelRel;
import com.suning.sawp.po.mainpushnew.StoreTask;
import com.suning.sawp.vo.mainpushnew.StaffAssginDetailVo;
import com.suning.sawp.vo.mainpushnew.StaffAssginParam;

@Service
public class StaffMainPushServiceImpl implements StaffMainPushService {

    private static Logger LOGGER = LoggerFactory.getLogger(StaffMainPushServiceImpl.class);
    
    @Resource
    StaffTaskDao staffTaskDao;
    
    @Override
    public List<StaffAssginDetailVo> querySaffAssginList(Long storeTaskId, Long modelId) {
        List<StaffAssginDetailVo> list = new ArrayList<StaffAssginDetailVo>();
        List<StaffModelRel> rels = staffTaskDao.queryStaffModelRels(storeTaskId, modelId);
        if (CollectionUtils.isNotEmpty(rels)) {
            for (StaffModelRel rel : rels) {
                StaffAssginDetailVo vo = new StaffAssginDetailVo();
                vo.setStaffId(rel.getStaffId());
                vo.setStaffName(rel.getStaffName());
                vo.setTargetNum(rel.getTargetNum());
                list.add(vo);
            }
        }
        return list;
    }

    @Override
    public ReturnMsg<String> assignStaffTask(StaffAssginParam assginParam) {
        ReturnMsg<String> returnMsg = checkParams(assginParam);
        if (null != assginParam && ReturnMsg.SUCCESS.equals(returnMsg.getRetFlag())) {
            // 参数校验通过
            // 查询门店任务
            StoreTask storeTask = staffTaskDao.queryStoreTaskByStoreTaskId(assginParam.getStoreTaskId());
            // 分配任务
            try {
                staffTaskDao.assignTask(assginParam, storeTask);
            } catch (Exception e) {
                LOGGER.error("StaffMainPushService.assignStaffTask error,params:{}", assginParam, e);
                returnMsg.setError(ErrorCodeConstants.ERR_0002);
            }
        }
        return returnMsg;
    }

    private ReturnMsg<String> checkParams(StaffAssginParam assginParam) {
        ReturnMsg<String> returnMsg = new ReturnMsg<String>();
        // 判断各项入参是否为空
        if (null != assginParam && null != assginParam.getModelId()
                && null != assginParam.getStoreTaskId() && CollectionUtils.isNotEmpty(assginParam.getTaskStoreMans())) {
            // 校验每个店员的分配台数是否>0
            int totalNum = 0;
            for (StaffAssginDetailVo vo : assginParam.getTaskStoreMans()) {
                if (!vo.validParams()) {
                    returnMsg.setError(ErrorCodeConstants.ERR_0001);
                    break;
                } else {
                    totalNum += vo.getTargetNum();
                }
            }
            // 校验店员被分配目标台数之和>=门店款数目标数，如果门店款数目标数为空则，店员被分配目标之和>0即可
            StoreModelRel storeRel = staffTaskDao.queryStoreModelRel(assginParam.getStoreTaskId(), assginParam.getModelId());
            if (null != storeRel && null != storeRel.getTargetNum() && totalNum < storeRel.getTargetNum()) {
                // 大区已经给门店指派该款数具体目标台数
                // 给店员分配的总台数《门店任务目标台数
                returnMsg.setError(ErrorCodeConstants.ERR_0043);
            }
        } else {
            returnMsg.setError(ErrorCodeConstants.ERR_0001);
        }
        
        return returnMsg;
    }
}
